<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\SettingHint
 *
 * @property int $id
 * @property int $type 类型,不同类型对应不同的显示位置
 * @property string $channel_code 对应setting_channel表的资方编号
 * @property string $content 具体的内容
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint whereChannelCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingHint whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class SettingHint extends Model
{

    /*
    *
      1 => ['msg' => '首页-不能借款'],
      2 => ['msg' => '首页-还款试算'],
      3 => ['msg' => '提单-页面下方'],
      4 => ['msg' => '提单-借款用途-每期还款'],
      5 => ['msg' => '审核已提交页-下方'],
      6 => ['msg' => '关注公众号'],
      7 => ['msg' => '公众号弹出框'],
      8 => ['msg' => '审核通过-每期还款'],
      9 => ['msg' => '审核通过-借款人综合险'],
      10 => ['msg' => '审核通过-下方'],
      11 => ['msg' => '收款超时-下方'],
      12 => ['msg' => '放款中-每期还款(含放款失败页)'],
      13 => ['msg' => '放款中-时长提示'],
      14 => ['msg' => '放款中-下方'],
      15 => ['msg' => '关于我们-了解按钮'],
      16 => ['msg' => '关于我们-客服电话'],
      17 => ['msg' => '关于我们-下方'],
      18 => ['msg' => '还款计划表-其他'],
      19 => ['msg' => '公众号名称'],
     */
    const TYPE_ONE = 1;
    const TYPE_TWO = 2;
    const TYPE_THREE = 3;
    const TYPE_FOUR = 4;
    const TYPE_FIVE = 5;
    const TYPE_SIX = 6;
    const TYPE_SEVEN = 7;
    const TYPE_EIGHT = 8;
    const TYPE_NINE = 9;
    const TYPE_TEN = 10;
    const TYPE_ELEVEN = 11;
    const TYPE_TWELVE = 12;
    const TYPE_THIRTEEN = 13;
    const TYPE_FOURTEEN = 14;
    const TYPE_FIFTEEN = 15;
    const TYPE_SIXTEEN = 16;
    const TYPE_SEVENTEEN = 17;
    const TYPE_EIGHTEEN = 18;
    const TYPE_NINETEEN = 19;
    const TYPE_TWENTY = 20;
    const TYPE_TWENTYONE = 21;
    const TYPE_TWENTYTWO = 22;
    const TYPE_TWENTYTHREE = 23;

    //提示语的默认值
    static $HINT_LIST = [
        self::TYPE_ONE => '请于3个月后再次尝试借款',
        self::TYPE_TWO => '实际还款金额以借款明细为准',
        self::TYPE_THREE => 'XXX承诺保证您的信息安全',
        self::TYPE_FOUR => '用户您好,本提示语可配置的',
        self::TYPE_FIVE => '我们将在30分钟内完成审核',
        self::TYPE_SIX => '关注公众号随时了解审核动向!',
        self::TYPE_SEVEN => '微信公众号，请在微信通讯录-搜索快贷公众号点击关注即可',
        self::TYPE_EIGHT => '每月应还本金+每月利息+第三方收取贷后管理费',
        self::TYPE_NINE => '用户您好,本提示语可配置的',
        self::TYPE_TEN => '还款日根据放款日期生成',
        self::TYPE_ELEVEN => '请在有效期内确认收款',
        self::TYPE_TWELVE => '用户您好,本提示语可配置的',
        self::TYPE_THIRTEEN => '款项将在24小时到账',
        self::TYPE_FOURTEEN => '如有疑问可点击客服咨询或拨打电话:400-0066265',
        self::TYPE_FIFTEEN => '了解XXX',
        self::TYPE_SIXTEEN => '400-827-8766',
        self::TYPE_SEVENTEEN => '公司版权信息',
        self::TYPE_EIGHTEEN => '由第三方平台收取贷后管理费用,用户您的账户资金及还款管理',
        self::TYPE_NINETEEN => '快闪卡贷',
        self::TYPE_TWENTY => '关于我们-了解H5链接url',
        self::TYPE_TWENTYONE => '申请意外保险',
        self::TYPE_TWENTYTWO => '谨防诈骗',
        self::TYPE_TWENTYTHREE => '请确认卡内余额充足，系统批扣时间为8:00以及下午13:00,建议3个小时后重试，切勿频繁操作',
    ];


    //
    protected $guarded = [
        'id'
    ];

    /**
     * Description:和document一个思路
     * Author:lijiafei
     * @return array|\Illuminate\Database\Eloquent\Collection|static[]
     */
    public static function getHintList(){

        $documentType = self::hintType();
        $settingChannel = new SettingChannel();
        $settingChannel->setConnection('backend');
        $channelList = $settingChannel->pluck('channel_name','channel_code');
        //判断资方有没有添加到特定类型中
        foreach ($documentType as $key => $val){
            if($val['is_channel_code']){
                if($channelList){
                    foreach ($channelList as $k => $v){
                        //判断当前数据库
                        $insertData = array();
                        $insertData['type'] = $key;
                        $insertData['channel_code'] = $k;
                        self::firstOrCreate($insertData);
                    }
                }
            }else{
                self::firstOrCreate(['type'=>$key]);
            }
        }

        $settingHint  = new SettingHint();
        $settingHint->setConnection('backend');
        $info = $settingHint->select('id','type','channel_code','content','status')
            ->orderBy('type','asc')
            ->get();

        //获取资方列表
        $info = $info->toArray();
        foreach ($info as $k => &$v){
            if(empty($documentType[$v['type']])){
                //删除这个多余的类型数据
                self::where('id',$v['id'])->delete();
                unset($info[$k]);
                continue;
            }else{
                $type = $documentType[$v['type']];
                $v['msg'] = $type['msg'];
            }
            if($documentType[$v['type']]['is_channel_code']){
                if(empty($channelList[$v['channel_code']])){
                    //删除这个多余的类型数据
                    self::where('id',$v['id'])->delete();
                    unset($info[$k]);
                    continue;
                }else{
                    $v['channel_name'] = $channelList[$v['channel_code']];
                }

            }
        }
        $info = array_values($info);
        return $info;
    }

    /**
     * Description: 获取类型获取
     * Author:lijiafei
     * @return array
     */
    public static function getHint($type = '',$channel_code = ''){
        $where = array();
        if(!empty($type)){
            $where[] = ['type','=',$type];
        }
        $list = (new SettingHint())->setConnection('backend')->select('type','channel_code','content')
            ->where($where)
            ->where('status',1)
            ->get()->toArray();
        $hintType = self::hintType();
        $returnData = array();
        foreach ($list as $k => $v){
            if(!empty($hintType[$v['type']])){
                $v['msg'] = $hintType[$v['type']]['msg'];
                if($hintType[$v['type']]['is_channel_code']){
                    //true,需要通过授权信息获取数据
                    if($channel_code == $v['channel_code']){
                        unset($v['channel_code']);
                        $returnData[] = $v;
                    }
                }else{
                    unset($v['channel_code']);
                    $returnData[] = $v;
                }
            }
        }
        return $returnData;
    }

    public static function hintType(){
        return $data = [
            self::TYPE_ONE => ['msg' => '首页-不能借款','is_channel_code' => false],
            self::TYPE_TWO => ['msg' => '首页-还款试算','is_channel_code' => false],
            self::TYPE_THREE => ['msg' => '提单-页面下方','is_channel_code' => false],
            self::TYPE_FOUR => ['msg' => '提单-借款用途-每期还款','is_channel_code' => false],
            self::TYPE_FIVE => ['msg' => '审核已提交页-下方','is_channel_code' => false],
            self::TYPE_SIX => ['msg' => '关注公众号','is_channel_code' => false],
            self::TYPE_SEVEN => ['msg' => '公众号弹出框','is_channel_code' => false],
            self::TYPE_EIGHT => ['msg' => '审核通过-每期还款','is_channel_code' => true],
            self::TYPE_NINE => ['msg' => '审核通过-借款人综合险','is_channel_code' => true],
            self::TYPE_TEN => ['msg' => '审核通过-下方','is_channel_code' => false],
            self::TYPE_ELEVEN => ['msg' => '收款超时-下方','is_channel_code' => false],
            self::TYPE_TWELVE => ['msg' => '放款中-每期还款(含放款失败页)','is_channel_code' => true],
            self::TYPE_THIRTEEN => ['msg' => '放款中-时长提示','is_channel_code' => false],
            self::TYPE_FOURTEEN => ['msg' => '放款中-下方','is_channel_code' => false],
            self::TYPE_FIFTEEN => ['msg' => '关于我们-了解按钮','is_channel_code' => false],
            self::TYPE_SIXTEEN => ['msg' => '关于我们-客服电话','is_channel_code' => false],
            self::TYPE_SEVENTEEN => ['msg' => '关于我们-下方','is_channel_code' => false],
            self::TYPE_EIGHTEEN => ['msg' => '还款计划表-其他','is_channel_code' => false],
            self::TYPE_NINETEEN => ['msg' => '公众号名称','is_channel_code' => false],
            self::TYPE_TWENTY => ['msg' => '关于我们-了解H5链接','is_channel_code' => false],
            self::TYPE_TWENTYONE => ['msg' => '申请意外保险','is_channel_code' => false],
            self::TYPE_TWENTYTWO => ['msg' => '谨防诈骗','is_channel_code' => false],
            self::TYPE_TWENTYTHREE => ['msg' => '还款提示语','is_channel_code' => false],
        ];
    }

}
